package activities;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Locale;

import com.iculture.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TimePicker;

public class AddMatchGroupActivity extends Activity {
	private Spinner spinnerIcon;
	private Button buttonStartDate;
	private Button buttonStartTime;
	private Button buttonActivityTime;
	private EditText editTextActivityName;
	private EditText editTextActivityPlace;
	private EditText editTextInvitePeople;
	private EditText editTextActivityPS;
	private ImageButton imageButtonGoogleMap;
	private ImageButton imageButtonPrevious;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_addmatchgroup);
		findView();
		setListenerOrAdapter();
	}

	private void findView() {
		spinnerIcon = (Spinner)findViewById(R.id.spinnerIcon);
		buttonStartDate = (Button)findViewById(R.id.buttonStartDate);
		buttonStartTime = (Button)findViewById(R.id.buttonStartTime);
		editTextActivityName = (EditText)findViewById(R.id.editTextActivityName);
		editTextActivityPlace = (EditText)findViewById(R.id.editTextActivityPlace);
		imageButtonGoogleMap = (ImageButton)findViewById(R.id.imageButtonGoogleMap);
		buttonActivityTime = (Button)findViewById(R.id.buttonActivityTime);
		editTextInvitePeople = (EditText)findViewById(R.id.editTextInvitePeople);
		editTextActivityPS = (EditText)findViewById(R.id.editTextActivityPS);
		imageButtonPrevious = (ImageButton)findViewById(R.id.imageButtonPrevious);
	}
	
	private void setListenerOrAdapter() {
		Integer[] icons = {R.drawable.birthday_icon, R.drawable.coffee_icon, R.drawable.eat_icon,
				R.drawable.exercise_icon, R.drawable.study_icon, R.drawable.music_icon};
		
		spinnerIcon.setAdapter(new SpinnerIconAdapter(AddMatchGroupActivity.this, android.R.layout.simple_spinner_dropdown_item, icons));
		editTextActivityName.setOnTouchListener(new EditTextActivityNameTouchListener());
		editTextActivityPlace.setOnTouchListener(new EditTextActivityPlaceTouchListener());
		imageButtonGoogleMap.setOnClickListener(new ImageButtonGoogleMapClickListener());
		buttonStartDate.setOnClickListener(new ButtonStartDateClickListener());
		buttonStartTime.setOnClickListener(new ButtonStartTimeClickListener());
		buttonActivityTime.setOnClickListener(new ButtonActivityTimeClickListener());
		editTextInvitePeople.setOnTouchListener(new EditTextInvitePeopleFocusTouchListener());
		editTextActivityPS.setOnTouchListener(new EditTextActivityPSTouchListener());
		imageButtonPrevious.setOnClickListener(new ImageButtonPreviousClickListener());
	}
	
	private class SpinnerIconAdapter extends ArrayAdapter<Integer> {
		public SpinnerIconAdapter(Context context, int textViewResourceId, Integer[] objects) {
			super(context, textViewResourceId, objects);
		}
		
		@Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }
 
        public View getCustomView(int position, View convertView, ViewGroup parent) {
        	View row = getLayoutInflater().inflate(R.layout.iconlist_row, parent, false);
            ((ImageView)row.findViewById(R.id.imageViewIcon)).setImageResource(getItem(position));
            return row;
        }
	}
	
	private class EditTextActivityNameTouchListener implements EditText.OnTouchListener {
		private boolean isTouched = false;

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			isTouched = editTextFirstTimeTouch(editTextActivityName, isTouched);
			return false;
		}
	}
	
	private class EditTextActivityPlaceTouchListener implements EditText.OnTouchListener {
		private boolean isTouched = false;

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			isTouched = editTextFirstTimeTouch(editTextActivityPlace, isTouched);
			return false;
		}
	}
	
	private class ImageButtonGoogleMapClickListener implements ImageButton.OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(AddMatchGroupActivity.this, GoogleMapActivity.class);
			
			startActivity(intent);
		}
	}
	
	private class ButtonStartDateClickListener implements Button.OnClickListener {
		private DatePicker startDatePicker;

		@Override
		public void onClick(View v) {
			Calendar now = Calendar.getInstance();
			
			startDatePicker = new DatePicker(AddMatchGroupActivity.this);
			startDatePicker.init(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE), null);
			startDatePicker.setCalendarViewShown(false);
			new AlertDialog.Builder(AddMatchGroupActivity.this)
			.setTitle("選擇開始日期")
			.setView(startDatePicker)
			.setNegativeButton(android.R.string.cancel, new StartDatePickerDialogNegativeClickListener())
			.setPositiveButton(android.R.string.ok, new StartDatePickerDialogPositiveClickListener())
			.show();
		}
		
		private class StartDatePickerDialogNegativeClickListener implements DialogInterface.OnClickListener {

			@Override
			public void onClick(DialogInterface dialog, int arg1) {
				dialog.cancel();
			}
		}
		
		private class StartDatePickerDialogPositiveClickListener implements DialogInterface.OnClickListener {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
				
				cal.set(startDatePicker.getYear(), startDatePicker.getMonth(), startDatePicker.getDayOfMonth());
				buttonStartDate.setText(sdf.format(cal.getTime()));
			}
		}
	}
	
	private class ButtonStartTimeClickListener implements Button.OnClickListener {
		private TimePicker startTimePicker;

		@Override
		public void onClick(View v) {
			Calendar now = Calendar.getInstance();
			
			startTimePicker = new TimePicker(AddMatchGroupActivity.this);
			startTimePicker.setIs24HourView(true);
			startTimePicker.setCurrentHour(now.get(Calendar.HOUR));
			startTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
			new AlertDialog.Builder(AddMatchGroupActivity.this)
			.setTitle("選擇開始時間")
			.setView(startTimePicker)
			.setNegativeButton(android.R.string.cancel, new StartTimePickerDialogNegativeClickListener())
			.setPositiveButton(android.R.string.ok, new StartTimePickerDialogPositiveClickListener())
			.show();
		}
		
		private class StartTimePickerDialogNegativeClickListener implements DialogInterface.OnClickListener {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		}
		
		private class StartTimePickerDialogPositiveClickListener implements DialogInterface.OnClickListener {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
				
				cal.set(Calendar.HOUR_OF_DAY, startTimePicker.getCurrentHour());
				cal.set(Calendar.MINUTE, startTimePicker.getCurrentMinute());
				buttonStartTime.setText(sdf.format(cal.getTime()));
			}
		}
	}
	
	private class ButtonActivityTimeClickListener implements Button.OnClickListener {
		private String[] timesList = new String[24];
		
		public ButtonActivityTimeClickListener() {
			for (int i=0; i<timesList.length; i++)
				timesList[i] = (i + 1) + "小時";
		}
		
		@Override
		public void onClick(View v) {
			new AlertDialog.Builder(AddMatchGroupActivity.this)
			.setTitle("選擇活動時間")
			.setItems(timesList, new ItemClickListener())
			.show();
		}
		
		private class ItemClickListener implements DialogInterface.OnClickListener {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				buttonActivityTime.setText(timesList[which]);
			}
		}
	}
	
	private class EditTextInvitePeopleFocusTouchListener implements EditText.OnTouchListener {
		private boolean isOpened = false;
		private String[] friends = {"Sam", "Jennifer", "Albert", "Veronica", "Joey"};
		private boolean[] checkedItems = new boolean[friends.length];
		private boolean[] checkedItemsTemp;
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (!isOpened) {
				checkedItemsTemp = Arrays.copyOf(checkedItems, checkedItems.length);
				new AlertDialog.Builder(AddMatchGroupActivity.this)
				.setTitle("請選擇朋友")
				.setMultiChoiceItems(friends, checkedItems, new InvitePeopleDialogMultiChoiceClickListener())
				.setNegativeButton(android.R.string.cancel, new InvitePeopleDialogNegativeClickListener())
				.setPositiveButton(android.R.string.ok, new InvitePeopleDialogPositiveClickListener())
				.setOnCancelListener(new InvitePeopleDialogCancelListener())
				.show();
				isOpened = true;
			}
			return true;
		}
		
		private class InvitePeopleDialogMultiChoiceClickListener implements DialogInterface.OnMultiChoiceClickListener {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
			}
		}
		
		private class InvitePeopleDialogNegativeClickListener implements DialogInterface.OnClickListener {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		}
		
		private class InvitePeopleDialogPositiveClickListener implements DialogInterface.OnClickListener {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String displayFriends = "";
				
				for (int i=0; i<checkedItems.length; i++) {
					if (checkedItems[i])
						displayFriends += friends[i] + " ";
				}
				editTextInvitePeople.setText(displayFriends);
				isOpened = false;
			}
		}
		
		private class InvitePeopleDialogCancelListener implements DialogInterface.OnCancelListener {

			@Override
			public void onCancel(DialogInterface dialog) {
				checkedItems = Arrays.copyOf(checkedItemsTemp, checkedItems.length);
				isOpened = false;
			}
		}
	}
	
	private class EditTextActivityPSTouchListener implements EditText.OnTouchListener {
		private boolean isTouched = false;

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			isTouched = editTextFirstTimeTouch(editTextActivityPS, isTouched);
			return false;
		}
	}
	
	private class ImageButtonPreviousClickListener implements ImageButton.OnClickListener {

		@Override
		public void onClick(View v) {
			AddMatchGroupActivity.this.finish();
		}
	}
	
	private boolean editTextFirstTimeTouch(EditText theText, boolean isTouched) {
		if (!isTouched)
			theText.setText("");
		return true;
	}
}
